package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    SmallVideosService smallVideosService;

    /**
     * 视频上传
     * POST
     * 请求参数：videoThumbnail  视频封面文件
     *         videoFile  视频文件
     * 返回值：null
     */
    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        smallVideosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     * 请求方式：GET
     * 请求参数。（Query）page  pagesize
     * 返回数据PageResult  List<VideoVo>
     */
    @GetMapping
    public ResponseEntity findVideo(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = smallVideosService.findVideo(page, pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 视频点赞
     */
    @PostMapping("/{videoId}/like")
    public ResponseEntity like(@PathVariable("videoId") String videoId) {
        //smallVideosService.like(videoId);
        Integer count = smallVideosService.like(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频点赞取消点赞
     * Post     /{videoId}/dislike
     * * 请求参数：  路径参数：id
     * * 返回数据：Integer  最新点赞数
     */
    @PostMapping("/{videoId}/dislike")
    public ResponseEntity dislike(@PathVariable("videoId") String videoId) {
        Integer count = smallVideosService.dislike(videoId);
        return ResponseEntity.ok(count);
    }

    /**
     * 视频评论列表
     * GET  /{videoId}/comments
     * 请求参数id、Query：
     * page
     * pagesize、
     * 返回数据：  PageResult
     */
    @GetMapping("/{videoId}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable("videoId") String videoId){
        PageResult vo = smallVideosService.findComments(page, pagesize, videoId);
        return ResponseEntity.ok(vo);
    }
    /**
     * 视频评论列表 视频评论
     * POST/{videoId}/comments
     * 请求参数：id Body:
     *  comment：评论正文
     */
    @PostMapping("/{videoId}/comments")
    public ResponseEntity saveComment(@RequestBody Map map,@PathVariable("videoId") String videoId){
        //获取请求参数
        String comment = map.get("comment").toString();
        String replace = videoId.replace("{", ""); //去除参数的大括号
        String replace1 = replace.replace("}", "");
        //定义定义service
        smallVideosService.saveComment(replace1,comment);
        return ResponseEntity.ok(null);
    }
}
